<!DOCTYPE HTML>
<html lang="en" >
    <!-- Start book 6节课入门Flask框架web开发 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>Flask中使用数据库 | 6节课入门Flask框架web开发</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../chapter3/shu-ju-ku-ji-ben-cao-zuo.html" />
    
    
    <link rel="prev" href="../chapter3.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="3.1"
        data-chapter-title="Flask中使用数据库"
        data-filepath="chapter3/shu-ju-ku-de-she-zhi.md"
        data-basepath=".."
        data-revision="Sun Jan 28 2018 01:59:46 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Flask快速入门课程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter1.html">
            
                
                    <a href="../chapter1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Flask基础
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="chapter1/flaskjian-jie.html">
            
                
                    <a href="../chapter1/flaskjian-jie.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        为什么要使用WEB框架
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="chapter1/flaskyu-django-dui-bi.html">
            
                
                    <a href="../chapter1/flaskyu-django-dui-bi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        Flask简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="chapter1/flask-an-zhuang-huan-jing.html">
            
                
                    <a href="../chapter1/flask-an-zhuang-huan-jing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        Flask 安装环境
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="chapter1/requirements-wen-jian.html">
            
                
                    <a href="../chapter1/requirements-wen-jian.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        requirements 文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="chapter1/hello-worldcheng-xu.html">
            
                
                    <a href="../chapter1/hello-worldcheng-xu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        Hello World程序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="chapter1/lu-you-de-ge-zhong-ding-yi-fang-shi.html">
            
                
                    <a href="../chapter1/lu-you-de-ge-zhong-ding-yi-fang-shi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        路由定义的基本方式
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter2.html">
            
                
                    <a href="../chapter2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="chapter2/jinja2mo-ban-yin-qing.html">
            
                
                    <a href="../chapter2/jinja2mo-ban-yin-qing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        Jinja2模板引擎
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="chapter2/guo-lv-qi.html">
            
                
                    <a href="../chapter2/guo-lv-qi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        过滤器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="chapter2/hong.html">
            
                
                    <a href="../chapter2/hong.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        Flask-WTF表单
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter3.html">
            
                
                    <a href="../chapter3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="3.1" data-path="chapter3/shu-ju-ku-de-she-zhi.html">
            
                
                    <a href="../chapter3/shu-ju-ku-de-she-zhi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        Flask中使用数据库
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="chapter3/shu-ju-ku-ji-ben-cao-zuo.html">
            
                
                    <a href="../chapter3/shu-ju-ku-ji-ben-cao-zuo.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        数据库基本操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="chapter3/zong-he-an-4f8b-tu-shu-guan-li.html">
            
                
                    <a href="../chapter3/zong-he-an-4f8b-tu-shu-guan-li.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        综合案例-图书管理
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.3.1" data-path="chapter3/zong-he-an-4f8b-tu-shu-guan-li/ding-yi-mo-xing.html">
            
                
                    <a href="../chapter3/zong-he-an-4f8b-tu-shu-guan-li/ding-yi-mo-xing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.1.</b>
                        
                        定义模型&amp;创建表&amp;数据添加
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.2" data-path="chapter3/zong-he-an-4f8b-tu-shu-guan-li/shu-ju-xian-793a26-biao-dan-tian-jia.html">
            
                
                    <a href="../chapter3/zong-he-an-4f8b-tu-shu-guan-li/shu-ju-xian-793a26-biao-dan-tian-jia.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.2.</b>
                        
                        数据显示&amp;表单添加
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.3" data-path="chapter3/zong-he-an-4f8b-tu-shu-guan-li/biao-dan-yan-zheng.html">
            
                
                    <a href="../chapter3/zong-he-an-4f8b-tu-shu-guan-li/biao-dan-yan-zheng.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.3.</b>
                        
                        添加数据的表单验证
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.4" data-path="chapter3/zong-he-an-4f8b-tu-shu-guan-li/shan-chu-shu-ju.html">
            
                
                    <a href="../chapter3/zong-he-an-4f8b-tu-shu-guan-li/shan-chu-shu-ju.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.4.</b>
                        
                        删除数据
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >6节课入门Flask框架web开发</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="flask&#x4E2D;&#x4F7F;&#x7528;&#x6570;&#x636E;&#x5E93;">Flask&#x4E2D;&#x4F7F;&#x7528;&#x6570;&#x636E;&#x5E93;</h1>
<h3 id="flasksqlalchemy&#x6269;&#x5C55;">Flask-SQLAlchemy&#x6269;&#x5C55;</h3>
<ul>
<li>SQLALchemy &#x5B9E;&#x9645;&#x4E0A;&#x662F;&#x5BF9;&#x6570;&#x636E;&#x5E93;&#x7684;&#x62BD;&#x8C61;&#xFF0C;&#x8BA9;&#x5F00;&#x53D1;&#x8005;&#x4E0D;&#x7528;&#x76F4;&#x63A5;&#x548C; SQL &#x8BED;&#x53E5;&#x6253;&#x4EA4;&#x9053;&#xFF0C;&#x800C;&#x662F;&#x901A;&#x8FC7; Python &#x5BF9;&#x8C61;&#x6765;&#x64CD;&#x4F5C;&#x6570;&#x636E;&#x5E93;&#xFF0C;&#x5728;&#x820D;&#x5F03;&#x4E00;&#x4E9B;&#x6027;&#x80FD;&#x5F00;&#x9500;&#x7684;&#x540C;&#x65F6;&#xFF0C;&#x6362;&#x6765;&#x7684;&#x662F;&#x5F00;&#x53D1;&#x6548;&#x7387;&#x7684;&#x8F83;&#x5927;&#x63D0;&#x5347;</li>
<li>SQLAlchemy&#x662F;&#x4E00;&#x4E2A;&#x5173;&#x7CFB;&#x578B;&#x6570;&#x636E;&#x5E93;&#x6846;&#x67B6;&#xFF0C;&#x5B83;&#x63D0;&#x4F9B;&#x4E86;&#x9AD8;&#x5C42;&#x7684;ORM&#x548C;&#x5E95;&#x5C42;&#x7684;&#x539F;&#x751F;&#x6570;&#x636E;&#x5E93;&#x7684;&#x64CD;&#x4F5C;&#x3002;flask-sqlalchemy&#x662F;&#x4E00;&#x4E2A;&#x7B80;&#x5316;&#x4E86;SQLAlchemy&#x64CD;&#x4F5C;&#x7684;flask&#x6269;&#x5C55;&#x3002;</li>
</ul>
<h3 id="&#x5B89;&#x88C5;-flasksqlalchemy">&#x5B89;&#x88C5; flask-sqlalchemy</h3>
<pre><code>pip install flask-sqlalchemy
</code></pre><p>&#x5982;&#x679C;&#x8FDE;&#x63A5;&#x7684;&#x662F;mysql&#x6570;&#x636E;&#x5E93;,&#x9700;&#x8981;&#x5B89;&#x88C5;mysqldb</p>
<pre><code>pip install flask-mysqldb
</code></pre><h3 id="&#x4F7F;&#x7528;flasksqlalchemy&#x7BA1;&#x7406;&#x6570;&#x636E;&#x5E93;">&#x4F7F;&#x7528;Flask-SQLAlchemy&#x7BA1;&#x7406;&#x6570;&#x636E;&#x5E93;</h3>
<p>&#x5728;Flask-SQLAlchemy&#x4E2D;&#xFF0C;&#x6570;&#x636E;&#x5E93;&#x4F7F;&#x7528;URL&#x6307;&#x5B9A;&#xFF0C;&#x800C;&#x4E14;&#x7A0B;&#x5E8F;&#x4F7F;&#x7528;&#x7684;&#x6570;&#x636E;&#x5E93;&#x5FC5;&#x987B;&#x4FDD;&#x5B58;&#x5230;Flask&#x914D;&#x7F6E;&#x5BF9;&#x8C61;&#x7684;SQLALCHEMY_DATABASE_URI&#x952E;&#x4E2D;&#x3002;</p>
<h4 id="flask&#x7684;&#x6570;&#x636E;&#x5E93;&#x8BBE;&#x7F6E;&#xFF1A;">Flask&#x7684;&#x6570;&#x636E;&#x5E93;&#x8BBE;&#x7F6E;&#xFF1A;</h4>
<pre><code class="lang-python">app.config[<span class="hljs-string">&apos;SQLALCHEMY_DATABASE_URI&apos;</span>] = <span class="hljs-string">&apos;mysql://root:mysql@127.0.0.1:3306/test&apos;</span>
</code></pre>
<p>&#x5176;&#x4ED6;&#x8BBE;&#x7F6E;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x52A8;&#x6001;&#x8FFD;&#x8E2A;&#x4FEE;&#x6539;&#x8BBE;&#x7F6E;&#xFF0C;&#x5982;&#x672A;&#x8BBE;&#x7F6E;&#x53EA;&#x4F1A;&#x63D0;&#x793A;&#x8B66;&#x544A;, &#x4E0D;&#x5EFA;&#x8BAE;&#x5F00;&#x542F;</span>
app.config[<span class="hljs-string">&apos;SQLALCHEMY_TRACK_MODIFICATIONS&apos;</span>] = <span class="hljs-keyword">False</span>
<span class="hljs-comment"># &#x67E5;&#x8BE2;&#x65F6;&#x4F1A;&#x663E;&#x793A;&#x539F;&#x59CB;SQL&#x8BED;&#x53E5;</span>
app.config[<span class="hljs-string">&apos;SQLALCHEMY_ECHO&apos;</span>] = <span class="hljs-keyword">True</span>
</code></pre>
<table>
<thead>
<tr>
<th style="text-align:left">&#x540D;&#x5B57;</th>
<th style="text-align:left">&#x5907;&#x6CE8;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">SQLALCHEMY_DATABASE_URI</td>
<td style="text-align:left">&#x7528;&#x4E8E;&#x8FDE;&#x63A5;&#x7684;&#x6570;&#x636E;&#x5E93; URI &#x3002;&#x4F8B;&#x5982;:sqlite:////tmp/test.dbmysql://username:password@server/db</td>
</tr>
<tr>
<td style="text-align:left">SQLALCHEMY_BINDS</td>
<td style="text-align:left">&#x4E00;&#x4E2A;&#x6620;&#x5C04; binds &#x5230;&#x8FDE;&#x63A5; URI &#x7684;&#x5B57;&#x5178;&#x3002;&#x66F4;&#x591A; binds &#x7684;&#x4FE1;&#x606F;&#x89C1;<a href="http://docs.jinkan.org/docs/flask-sqlalchemy/binds.html#binds" target="_blank"><em>&#x7528; Binds &#x64CD;&#x4F5C;&#x591A;&#x4E2A;&#x6570;&#x636E;&#x5E93;</em></a>&#x3002;</td>
</tr>
<tr>
<td style="text-align:left">SQLALCHEMY_ECHO</td>
<td style="text-align:left">&#x5982;&#x679C;&#x8BBE;&#x7F6E;&#x4E3A;Ture&#xFF0C; SQLAlchemy &#x4F1A;&#x8BB0;&#x5F55;&#x6240;&#x6709; &#x53D1;&#x7ED9; stderr &#x7684;&#x8BED;&#x53E5;&#xFF0C;&#x8FD9;&#x5BF9;&#x8C03;&#x8BD5;&#x6709;&#x7528;&#x3002;(&#x6253;&#x5370;sql&#x8BED;&#x53E5;)</td>
</tr>
<tr>
<td style="text-align:left">SQLALCHEMY_RECORD_QUERIES</td>
<td style="text-align:left">&#x53EF;&#x4EE5;&#x7528;&#x4E8E;&#x663E;&#x5F0F;&#x5730;&#x7981;&#x7528;&#x6216;&#x542F;&#x7528;&#x67E5;&#x8BE2;&#x8BB0;&#x5F55;&#x3002;&#x67E5;&#x8BE2;&#x8BB0;&#x5F55; &#x5728;&#x8C03;&#x8BD5;&#x6216;&#x6D4B;&#x8BD5;&#x6A21;&#x5F0F;&#x81EA;&#x52A8;&#x542F;&#x7528;&#x3002;&#x66F4;&#x591A;&#x4FE1;&#x606F;&#x89C1;get_debug_queries()&#x3002;</td>
</tr>
<tr>
<td style="text-align:left">SQLALCHEMY_NATIVE_UNICODE</td>
<td style="text-align:left">&#x53EF;&#x4EE5;&#x7528;&#x4E8E;&#x663E;&#x5F0F;&#x7981;&#x7528;&#x539F;&#x751F; unicode &#x652F;&#x6301;&#x3002;&#x5F53;&#x4F7F;&#x7528; &#x4E0D;&#x5408;&#x9002;&#x7684;&#x6307;&#x5B9A;&#x65E0;&#x7F16;&#x7801;&#x7684;&#x6570;&#x636E;&#x5E93;&#x9ED8;&#x8BA4;&#x503C;&#x65F6;&#xFF0C;&#x8FD9;&#x5BF9;&#x4E8E; &#x4E00;&#x4E9B;&#x6570;&#x636E;&#x5E93;&#x9002;&#x914D;&#x5668;&#x662F;&#x5FC5;&#x987B;&#x7684;&#xFF08;&#x6BD4;&#x5982; Ubuntu &#x4E0A; &#x67D0;&#x4E9B;&#x7248;&#x672C;&#x7684; PostgreSQL &#xFF09;&#x3002;</td>
</tr>
<tr>
<td style="text-align:left">SQLALCHEMY_POOL_SIZE</td>
<td style="text-align:left">&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;&#x6C60;&#x7684;&#x5927;&#x5C0F;&#x3002;&#x9ED8;&#x8BA4;&#x662F;&#x5F15;&#x64CE;&#x9ED8;&#x8BA4;&#x503C;&#xFF08;&#x901A;&#x5E38; &#x662F; 5 &#xFF09;</td>
</tr>
<tr>
<td style="text-align:left">SQLALCHEMY_POOL_TIMEOUT</td>
<td style="text-align:left">&#x8BBE;&#x5B9A;&#x8FDE;&#x63A5;&#x6C60;&#x7684;&#x8FDE;&#x63A5;&#x8D85;&#x65F6;&#x65F6;&#x95F4;&#x3002;&#x9ED8;&#x8BA4;&#x662F; 10 &#x3002;</td>
</tr>
<tr>
<td style="text-align:left">SQLALCHEMY_POOL_RECYCLE</td>
<td style="text-align:left">&#x591A;&#x5C11;&#x79D2;&#x540E;&#x81EA;&#x52A8;&#x56DE;&#x6536;&#x8FDE;&#x63A5;&#x3002;&#x8FD9;&#x5BF9; MySQL &#x662F;&#x5FC5;&#x8981;&#x7684;&#xFF0C; &#x5B83;&#x9ED8;&#x8BA4;&#x79FB;&#x9664;&#x95F2;&#x7F6E;&#x591A;&#x4E8E; 8 &#x5C0F;&#x65F6;&#x7684;&#x8FDE;&#x63A5;&#x3002;&#x6CE8;&#x610F;&#x5982;&#x679C; &#x4F7F;&#x7528;&#x4E86; MySQL &#xFF0C; Flask-SQLALchemy &#x81EA;&#x52A8;&#x8BBE;&#x5B9A; &#x8FD9;&#x4E2A;&#x503C;&#x4E3A; 2 &#x5C0F;&#x65F6;&#x3002;</td>
</tr>
</tbody>
</table>
<h3 id="&#x5E38;&#x7528;&#x7684;sqlalchemy&#x5B57;&#x6BB5;&#x7C7B;&#x578B;">&#x5E38;&#x7528;&#x7684;SQLAlchemy&#x5B57;&#x6BB5;&#x7C7B;&#x578B;</h3>
<table>
<thead>
<tr>
<th style="text-align:left">&#x7C7B;&#x578B;&#x540D;</th>
<th style="text-align:left">python&#x4E2D;&#x7C7B;&#x578B;</th>
<th style="text-align:left">&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">Integer</td>
<td style="text-align:left">int</td>
<td style="text-align:left">&#x666E;&#x901A;&#x6574;&#x6570;&#xFF0C;&#x4E00;&#x822C;&#x662F;32&#x4F4D;</td>
</tr>
<tr>
<td style="text-align:left">SmallInteger</td>
<td style="text-align:left">int</td>
<td style="text-align:left">&#x53D6;&#x503C;&#x8303;&#x56F4;&#x5C0F;&#x7684;&#x6574;&#x6570;&#xFF0C;&#x4E00;&#x822C;&#x662F;16&#x4F4D;</td>
</tr>
<tr>
<td style="text-align:left">BigInteger</td>
<td style="text-align:left">int&#x6216;long</td>
<td style="text-align:left">&#x4E0D;&#x9650;&#x5236;&#x7CBE;&#x5EA6;&#x7684;&#x6574;&#x6570;</td>
</tr>
<tr>
<td style="text-align:left">Float</td>
<td style="text-align:left">float</td>
<td style="text-align:left">&#x6D6E;&#x70B9;&#x6570;</td>
</tr>
<tr>
<td style="text-align:left">Numeric</td>
<td style="text-align:left">decimal.Decimal</td>
<td style="text-align:left">&#x666E;&#x901A;&#x6574;&#x6570;&#xFF0C;&#x4E00;&#x822C;&#x662F;32&#x4F4D;</td>
</tr>
<tr>
<td style="text-align:left">String</td>
<td style="text-align:left">str</td>
<td style="text-align:left">&#x53D8;&#x957F;&#x5B57;&#x7B26;&#x4E32;</td>
</tr>
<tr>
<td style="text-align:left">Text</td>
<td style="text-align:left">str</td>
<td style="text-align:left">&#x53D8;&#x957F;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x5BF9;&#x8F83;&#x957F;&#x6216;&#x4E0D;&#x9650;&#x957F;&#x5EA6;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x505A;&#x4E86;&#x4F18;&#x5316;</td>
</tr>
<tr>
<td style="text-align:left">Unicode</td>
<td style="text-align:left">unicode</td>
<td style="text-align:left">&#x53D8;&#x957F;Unicode&#x5B57;&#x7B26;&#x4E32;</td>
</tr>
<tr>
<td style="text-align:left">UnicodeText</td>
<td style="text-align:left">unicode</td>
<td style="text-align:left">&#x53D8;&#x957F;Unicode&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x5BF9;&#x8F83;&#x957F;&#x6216;&#x4E0D;&#x9650;&#x957F;&#x5EA6;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x505A;&#x4E86;&#x4F18;&#x5316;</td>
</tr>
<tr>
<td style="text-align:left">Boolean</td>
<td style="text-align:left">bool</td>
<td style="text-align:left">&#x5E03;&#x5C14;&#x503C;</td>
</tr>
<tr>
<td style="text-align:left">Date</td>
<td style="text-align:left">datetime.date</td>
<td style="text-align:left">&#x65F6;&#x95F4;</td>
</tr>
<tr>
<td style="text-align:left">Time</td>
<td style="text-align:left">datetime.datetime</td>
<td style="text-align:left">&#x65E5;&#x671F;&#x548C;&#x65F6;&#x95F4;</td>
</tr>
<tr>
<td style="text-align:left">LargeBinary</td>
<td style="text-align:left">str</td>
<td style="text-align:left">&#x4E8C;&#x8FDB;&#x5236;&#x6587;&#x4EF6;</td>
</tr>
</tbody>
</table>
<h3 id="&#x5E38;&#x7528;&#x7684;sqlalchemy&#x5217;&#x9009;&#x9879;">&#x5E38;&#x7528;&#x7684;SQLAlchemy&#x5217;&#x9009;&#x9879;</h3>
<table>
<thead>
<tr>
<th style="text-align:left">&#x9009;&#x9879;&#x540D;</th>
<th style="text-align:left">&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">primary_key</td>
<td style="text-align:left">&#x5982;&#x679C;&#x4E3A;True&#xFF0C;&#x4EE3;&#x8868;&#x8868;&#x7684;&#x4E3B;&#x952E;</td>
</tr>
<tr>
<td style="text-align:left">unique</td>
<td style="text-align:left">&#x5982;&#x679C;&#x4E3A;True&#xFF0C;&#x4EE3;&#x8868;&#x8FD9;&#x5217;&#x4E0D;&#x5141;&#x8BB8;&#x51FA;&#x73B0;&#x91CD;&#x590D;&#x7684;&#x503C;</td>
</tr>
<tr>
<td style="text-align:left">index</td>
<td style="text-align:left">&#x5982;&#x679C;&#x4E3A;True&#xFF0C;&#x4E3A;&#x8FD9;&#x5217;&#x521B;&#x5EFA;&#x7D22;&#x5F15;&#xFF0C;&#x63D0;&#x9AD8;&#x67E5;&#x8BE2;&#x6548;&#x7387;</td>
</tr>
<tr>
<td style="text-align:left">nullable</td>
<td style="text-align:left">&#x5982;&#x679C;&#x4E3A;True&#xFF0C;&#x5141;&#x8BB8;&#x6709;&#x7A7A;&#x503C;&#xFF0C;&#x5982;&#x679C;&#x4E3A;False&#xFF0C;&#x4E0D;&#x5141;&#x8BB8;&#x6709;&#x7A7A;&#x503C;</td>
</tr>
<tr>
<td style="text-align:left">default</td>
<td style="text-align:left">&#x4E3A;&#x8FD9;&#x5217;&#x5B9A;&#x4E49;&#x9ED8;&#x8BA4;&#x503C;</td>
</tr>
</tbody>
</table>
<h3 id="&#x5E38;&#x7528;&#x7684;sqlalchemy&#x5173;&#x7CFB;&#x9009;&#x9879;">&#x5E38;&#x7528;&#x7684;SQLAlchemy&#x5173;&#x7CFB;&#x9009;&#x9879;</h3>
<table>
<thead>
<tr>
<th style="text-align:left">&#x9009;&#x9879;&#x540D;</th>
<th style="text-align:left">&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">backref</td>
<td style="text-align:left">&#x5728;&#x5173;&#x7CFB;&#x7684;&#x53E6;&#x4E00;&#x6A21;&#x578B;&#x4E2D;&#x6DFB;&#x52A0;&#x53CD;&#x5411;&#x5F15;&#x7528;</td>
</tr>
<tr>
<td style="text-align:left">primary join</td>
<td style="text-align:left">&#x660E;&#x786E;&#x6307;&#x5B9A;&#x4E24;&#x4E2A;&#x6A21;&#x578B;&#x4E4B;&#x95F4;&#x4F7F;&#x7528;&#x7684;&#x8054;&#x7ED3;&#x6761;&#x4EF6;</td>
</tr>
<tr>
<td style="text-align:left">uselist</td>
<td style="text-align:left">&#x5982;&#x679C;&#x4E3A;False&#xFF0C;&#x4E0D;&#x4F7F;&#x7528;&#x5217;&#x8868;&#xFF0C;&#x800C;&#x4F7F;&#x7528;&#x6807;&#x91CF;&#x503C;</td>
</tr>
<tr>
<td style="text-align:left">order_by</td>
<td style="text-align:left">&#x6307;&#x5B9A;&#x5173;&#x7CFB;&#x4E2D;&#x8BB0;&#x5F55;&#x7684;&#x6392;&#x5E8F;&#x65B9;&#x5F0F;</td>
</tr>
<tr>
<td style="text-align:left">secondary</td>
<td style="text-align:left">&#x6307;&#x5B9A;&#x591A;&#x5BF9;&#x591A;&#x4E2D;&#x8BB0;&#x5F55;&#x7684;&#x6392;&#x5E8F;&#x65B9;&#x5F0F;</td>
</tr>
<tr>
<td style="text-align:left">secondary join</td>
<td style="text-align:left">&#x5728;SQLAlchemy&#x4E2D;&#x65E0;&#x6CD5;&#x81EA;&#x884C;&#x51B3;&#x5B9A;&#x65F6;&#xFF0C;&#x6307;&#x5B9A;&#x591A;&#x5BF9;&#x591A;&#x5173;&#x7CFB;&#x4E2D;&#x7684;&#x4E8C;&#x7EA7;&#x8054;&#x7ED3;&#x6761;&#x4EF6;</td>
</tr>
</tbody>
</table>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../chapter3.html" class="navigation navigation-prev " aria-label="Previous page: 数据库"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../chapter3/shu-ju-ku-ji-ben-cao-zuo.html" class="navigation navigation-next " aria-label="Next page: 数据库基本操作"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"toggle-chapters":{},"splitter":{},"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book 6节课入门Flask框架web开发 -->
</html>
